import { getDictList, addDict, getDictListByDict, updateDict, getSingleDict, deleteDict } from '@/api/dict'
import { getToken } from '@/utils/auth'

const dict = {
  state: {
    dictList: [],
    singleItem: {},
    dictListTotalItem: '',
    dictByDict: [],
    roken: getToken() || ''
  },
  mutations: {
    SET_LIST: (state, data) => {
      state.dictList = data
    },
    SET_DICT_BY_DICT_LIST: (state, data) => {
      state.dictByDict = data
    },
    SET_DICT_LIST_TOTAL_ITEM: (state, data) => {
      state.dictListTotalItem = data
    },
    SET_SINGLE_ITEM: (state, data) => {
      state.singleItem = data
    }
  },
  actions: {
    FindDictByDict: ({ commit, state }, params) => {
      return new Promise((resolve, reject) => {
        getDictListByDict(state.token, params).then(response => {
          const data = response.data
          if (data && data.length > 0) { // 验证返回的Dict是否是一个非空数组
            commit('SET_DICT_BY_DICT_LIST', data)
          } else {
            reject('getDictListFailed: list must be a non-null array !')
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    GetDictList: ({ commit, state }, params) => {
      return new Promise((resolve, reject) => {
        getDictList(state.token, params).then(response => {
          const data = response.data
          console.log(data);
          if (data.rows && data.rows.length > 0) { // 验证返回的Dict是否是一个非空数组
            commit('SET_LIST', data.rows)
            commit('SET_USER_LIST_TOTAL_ITEM', data.total)
          } else {
            reject('getDictlistFailed: list must be a non-null array !')
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    AddDict: ({ state }, params) => {
      return new Promise((resolve, reject) => {
        addDict(state.token, params).then(res => {
          if (res.result === 'success') {
            resolve(res)
          } else {
            reject(res)
          }
        }).catch(err => {
          reject(err)
        })
      })
    },

    UpdateDict: ({ state }, params) => {
      return new Promise((resolve, reject) => {
        updateDict(state.token, params).then(res => {
          if (res.result === 'success') {
            resolve(res)
          } else {
            reject(res)
          }
        }).catch(err => {
          reject(err)
        })
      })
    },

    GetSingleDict: ({ commit, state }, params) => {
      return new Promise((resolve, reject) => {
        getSingleDict(state.token, params).then(res => {
          if (res.result === 'success') {
            commit('SET_SINGLE_ITEM', res.data)
            resolve(res)
          } else {
            reject(res)
          }
        }).catch(err => {
          reject(err)
        })
      })
    },

    DeleteDict: ({ state }, params) => {
      return new Promise((resolve, reject) => {
        deleteDict(state.token, params).then(res => {
          if (res.result === 'success') {
            resolve(res)
          } else {
            reject(res)
          }
        }).catch(err => {
          reject(err)
        })
      })
    }
  }
}

export default dict
